<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Article extends Common
{
    // 文章列表页面
    public function index() {
        //
        $article = db('article')->paginate();
        $this->assign('article', $article);
        return view();
    }

    // 添加/编辑文章
    public function save_article() {
        $id = input('param.id');
        $cates = get_cates('article_category', 0);
        $this->assign('cates', $cates);
        if (Request::instance()->isPost()) {
            // 提交数据
            $data = [
                'name' => input('param.name'),
                'cate_id' => input('param.cate_id', 0),
                'add_time' => time()
            ];

            $post_image = input('param.image', '');
            if ($post_image) {
                // 如果图片有值  则获取旧图并删除
                $image = db('article')->where('id', $id)->value('image');
                if ($image) {
                    unlink('./uploads/nav/'.$image);
                }
                $data['image'] = $post_image;
            }

            if ($id > 0)  {
                $text = '编辑';
                $rs = db('article')->where('id', $id)->update($data);
                $url = url('index');
                db('article_content')->where('article_id', $id)->update(['content'=> input('param.content', ''), 'info' => input('param.info', '')]);
            } else {
                $text = '添加';
                $rs = db('article')->insertGetId($data);
                $url = url('save_article', 'id=' . $rs);
                db('article_content')->insert(['content'=> input('param.content', ''), 'article_id' => $rs]);
            }
            if ($rs)
                return json(['code' => 200, 'msg' => $text . '案例成功！', 'url' => $url]);
            else
                return json(['code' => 0, 'msg' => $text . '案例失败！']);
        } else {
            if ($id > 0) {
                $article = db('article')->alias('a')->field('a.*, ac.content, ac.info')
                    ->join('osl_article_content ac', 'a.id = ac.article_id', 'LEFT')->where('a.id', $id)->find();
                $this->assign('article', $article);
            }

            return view();
        }
    }

    // 文章分类
    public function article_category() {
        $cate = get_cates('article_category', 0);
        $this->assign('cate', $cate);
        return view();
    }

    // 添加/编辑文章分类
    public function save_category() {
        $id = input('param.id', 0);
        $cates = get_cates('article_category', 0);
        $this->assign('cates', $cates);
        if (Request::instance()->isPost()) {
            // 提交数据
            $data = [
                'pid' => input('param.pid'),
                'name' => input('param.name'),
                'level' => 1
            ];
            if (input('param.pid') > 0) {
                $cate = db('article_category')->where('id', input('param.pid'))->find();
                if ($cate['level'] >= 2) {
                    return json(['code' => 0, 'msg' => '分类最多二级，请确认！']);
                } else{
                    $data['level'] = $cate['level'] + 1;
                }
            }

            if ($id > 0)  {
                $text = '编辑';
                $rs = db('article_category')->where('id', $id)->update($data);
            } else {
                $text = '添加';
                $rs = db('article_category')->insert($data);
            }
            if ($rs !== false)
                return json(['code' => 200, 'msg' => $text . '文章分类成功！']);
            else
                return json(['code' => 0, 'msg' => $text . '文章分类失败！']);
        } else {
            $text = $id > 0 ? '编辑' : '添加';
            $cate = [];
            if ($id > 0) {
                $cate = db('article_category')->where('id', $id)->find();
            }
            $this->assign('cate', $cate);
            $this->assign('text', $text);
            return view();
        }
    }

    // 删除分类
    public function del_cate() {
        $id = input('param.id', 0);
        $article = db('article')->where('cate_id', $id)->count();
        if ($article > 0) {
            return json(['code' => 0, 'msg' => '分类下面有文章，不能删除！']);
        }

        $category = db('article_category')->where('pid', $id)->count();
        if ($category > 0) {
            return json(['code' => 0, 'msg' => '分类下面有子类，不能删除！']);
        }
        $rs = db('article_category')->where('id', $id)->delete();
        if ($rs !== false) {
            return json(['code' => 200, 'msg' => '删除成功！']);
        } else {
            return json(['code' => 0, 'msg' => '删除失败！']);
        }
    }
}
